package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import client.Catalog;
import client.CatalogManaging;
import client.ClientConsole;
import client.Main;
import client.Search_Control;
import client.setImage;


public class Search_GUI extends JFrame implements ActionListener 
{
	final public static int DEFAULT_PORT = 5555;
	public Search_Control search = null;  
	public static ClientConsole conn1 = null;
	public setImage img = null;;
	public JButton Back = null;
	public JButton Search = null;
	public JButton viewCatalog = null;
	public JLabel title = null; 
	public JLabel fromPricel = null; 
	public JLabel upToPricel = null; 
	public JLabel itemIdl = null; 
	public JLabel colorl = null; 
	public JLabel typel = null; 
	public JTextField fromPrice  = null;
	public JTextField upToPrice  = null;
	public JTextField itemId  = null;
	public JTextField color  = null;
	public JLabel info  = null;
	public JButton type = null;
	public JButton logOut = null;
	public String callFrom = null;
	
	List typesList = null;
	List colorList = null;
	public static String[] types = {"sidurprahim", "zerkala", "zer", "achich", "perah"};
	String[] typesSelected = null;
	public static String[] colors = {"gray", "red", "white", "brown", "green", "orange", "yellow", "blue", "violet", "pink"};
	String[] colorsSelected = null;
	
	public Search_GUI(String call)
	{
		callFrom = call;
		img = new setImage("Search");
		img.setLayout(null);
		this.setPreferredSize(new Dimension(400, 500));
		this.setBounds((int)Main.dim.getWidth()/2-200, (int)Main.dim.getHeight()/2-250, 400, 500);
		this.setResizable(false);
		
		title=new JLabel();
		title.setText("Zer-Lah Search");
		title.setBounds(110, 55, 200, 40);
		title.setForeground(Color.DARK_GRAY);
		title.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,25));
		img.add(title);
		
		logOut = new JButton("LogOut");
		logOut.setBounds(10,10,100,40);
		logOut.addActionListener(this);
		img.add(logOut);
		
		viewCatalog = new JButton("Catalog");
		viewCatalog.setBounds(150,10,100,40);
		viewCatalog.addActionListener(this);
		
		if(callFrom.equals("order_GUI"))	
			img.add(viewCatalog);
		
		Back = new JButton("Back");
		Back.setBounds(290,10,100,40);
		Back.addActionListener(this);
		img.add(Back);
		
		fromPricel=new JLabel();
		fromPricel.setText("Min Price:");
		fromPricel.setBounds(40, 105, 150, 40);
		fromPricel.setForeground(Color.DARK_GRAY);
		fromPricel.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,22));
		img.add(fromPricel);
		
		fromPrice=new JTextField();
		fromPrice.setBounds(200, 105, 150, 40);
		fromPrice.setForeground(Color.DARK_GRAY);
		fromPrice.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,18));
		img.add(fromPrice);
		
		upToPricel=new JLabel();
		upToPricel.setText("Max Price:");
		upToPricel.setBounds(40, 155, 150, 40);
		upToPricel.setForeground(Color.DARK_GRAY);
		upToPricel.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,22));
		img.add(upToPricel);
		
		upToPrice=new JTextField();
		upToPrice.setBounds(200, 155, 150, 40);
		upToPrice.setForeground(Color.DARK_GRAY);
		upToPrice.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,18));
		img.add(upToPrice);
		
		
		itemIdl=new JLabel();
		itemIdl.setText("Item Id:");
		itemIdl.setBounds(40, 205, 150, 40);
		itemIdl.setForeground(Color.DARK_GRAY);
		itemIdl.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,22));
		img.add(itemIdl);
		
		itemId=new JTextField();
		itemId.setBounds(200, 205, 150, 40);
		itemId.setForeground(Color.DARK_GRAY);
		itemId.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,18));
		img.add(itemId);
		
		colorl=new JLabel();
		colorl.setText("Color:");
		colorl.setBounds(260, 255, 150, 40);
		colorl.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,22));
		colorl.setForeground(Color.DARK_GRAY);
		img.add(colorl);
		
		colorList = new List();
		colorList.setBounds(220, 295, 150, 85);
		colorList.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		colorList.setMultipleMode(true);
		colorList.addActionListener(this);
		img.add(colorList);
		
		for(int i=0;i<colors.length;i++)
			colorList.add(colors[i]);
		
		typel=new JLabel();
		typel.setText("type:");
		typel.setBounds(70, 255, 150, 40);
		typel.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,22));
		typel.setForeground(Color.DARK_GRAY);
		img.add(typel);
		
		typesList = new List();
		typesList.setBounds(30, 295, 150, 85);
		typesList.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		typesList.setMultipleMode(true);
		typesList.addActionListener(this);
		img.add(typesList);
		
		for(int i=0;i<types.length;i++)
			typesList.add(types[i]);
		
		info = new JLabel("You can set multiple choices");
		info.setBounds(60,430,340,30);
		info.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,20));
		info.setForeground(Color.DARK_GRAY);
		img.add(info);
		
		Search = new JButton("Search");
		Search.setBounds(30,390,340,40);
		Search.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,20));
		Search.addActionListener(this);
		img.add(Search);
		
		this.add(img);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.pack();
		this.setVisible(true);
	}
	
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == Search)
		{
			typesSelected = typesList.getSelectedItems();
			colorsSelected = colorList.getSelectedItems();
			search = new Search_Control(fromPrice.getText(), upToPrice.getText(), itemId.getText(), colorsSelected, typesSelected, "search");
			
			if(search.counter != search.NumberOfSearchFields && search.flag != false)
			{	
				if(Search_Control.list.size() != 0)
				{	
					if(callFrom.equals("MainScreen"))
					{	
						if(Main.catalog != null)
							Main.catalog.dispose();
						Main.catalog = new Catalog("Search");
					}
					if(callFrom.equals("order_GUI"))
					{	
						if(Main.catalog != null)
							Main.catalog.dispose();
						Main.catalog = new Catalog("order_GUI");
					}
					if(callFrom.equals("CatalogManaging"))
					{
						if(Main.main.catalogManag != null)
							Main.main.catalogManag.dispose();
						Main.main.catalogManag = new CatalogManaging("Search");
					}
					this.setVisible(false);
				}
				else 
					JOptionPane.showMessageDialog(null,"No results","ERROR",JOptionPane.ERROR_MESSAGE);		
			}
		}
		
		if(e.getSource() == viewCatalog)
		{
			search = new Search_Control("-1", "-1", "-1", Search_GUI.colors, Search_GUI.types, "catalog");
			if(Main.catalog != null)
				Main.catalog.dispose();
			Main.catalog = new Catalog("order_GUI");
			this.setVisible(false);
			Main.main.order.setVisible(false);
		}
		
		if(e.getSource() == Back)
		{
			if(callFrom.equals("MainScreen"))
			{
				Main.main.setEnabled(true);
				Main.main.setVisible(true);
			}
			if(callFrom.equals("CatalogManaging"))
			{
				Main.main.catalogManag.setEnabled(true);
				Main.main.callFrom = "MainScreen";
			}
			if(callFrom.equals("order_GUI"))
			{
				  Main.main.order.setEnabled(true);
				  Main.main.order.setVisible(true);
			}
			this.dispose();
		}
		if(e.getSource() == logOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
	}
}
